<template>
  <div :class="{active: isActive, error: isError}" id="continers">
    <div class="menubar" @touchmove.prevent>

      <div class="menubar-bg" :class="xd_flag&&'xd-active'"></div>
      <div class="logo">
        <h4 style="color:whitesmoke;font-family: bold;margin-top:18px">Personcoder</h4>

      </div>

      <ul style="margin:0;padding:0;">
        <div class="xys-info">

          <img :src="user.avatar" alt="" style="width: 66px; border-radius: 55px;height: 66px">
          <div style="color:white;">{{ user.nickname }}</div>
          <hr>
        </div>
        <li><a href="#/front/home">
          <span class="icon" style="color: #ffffffcc;"> <i class="el-icon-s-home"></i></span>
          <span style="color:white" class="title" :class="index=='1'&&'nav-active'">首页</span>
        </a></li>
        <li><a href="#/front/studentExam">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-document"></i></span>
          <span style="color:white" class="title" :class="index=='2'&&'nav-active'">评测</span>
        </a></li>
        <li><a href="#/front/video">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-reading"></i></span>
          <span style="color:white" class="title" :class="index=='3'&&'nav-active'">学习中心</span>
        </a></li>
        <!--        <li><a href="#/dashboard">
                  <span class="icon" style="color: #ffffffcc;"><i class="el-icon-video-camera"></i></span>
                  <span style="color:white" class="title" :class="index=='4'&&'nav-active'">直播</span>
                </a></li>-->
        <li><a href="#/front/chat">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-s-promotion"></i></span>
          <span style="color:white" class="title" :class="index=='5'&&'nav-active'">聊天室</span>
        </a></li>
        <li><a href="#/patent">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-s-goods"></i></span>
          <span style="color:white" class="title" :class="index=='6'&&'nav-active'">商城</span>
        </a></li>
        <li><a href="#/conference">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-search"></i></span>
          <span style="color:white" class="title" :class="index=='7'&&'nav-active'">智搜</span>
        </a></li>
        <li><a>
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-picture"></i></span>
          <span style="color:white" class="title" :class="index=='8'&&'nav-active'">相册</span>
        </a></li>
        <li><a href="#/recommendation">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-message"></i></span>
          <span style="color:white" class="title" :class="index=='9'&&'nav-active'">留言</span>
        </a></li>
        <li v-if="user.role=='ROLE_ADMIN' || user.role=='ROLE_ROOT'"><a href="#/dashboard">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-s-platform"></i></span>
          <span style="color:white" class="title" :class="index=='10'&&'nav-active'">控制台</span>
        </a></li>
        <li v-show="isShow"><a href="#/front/person">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-s-custom"></i></span>
          <span style="color:white" class="title" :class="index=='11'&&'nav-active'">个人中心</span>
        </a></li>
        <li v-show="isShow" id="xxx"><a href="#/recommendation">
          <span class="icon" style="color: #ffffffcc;"><i class="el-icon-s-unfold"></i></span>
          <span style="color:white" class="title " :class="index=='9'&&'nav-active'" @click="logout">安全退出</span>
        </a></li>
      </ul>

      <div class="user">
        <div class="userimg">
          <img :src="user.avatar" alt="" style="margin-left: 15px">
        </div>
        <el-dropdown trigger="click">
        <span class="el-dropdown-link">
        <span class="username" style="color: whitesmoke">{{ user.nickname }}  <i
          class="el-icon-arrow-down el-icon--right"></i></span>
        </span>
          <el-dropdown-menu slot="dropdown" style="width:120px">
            <el-dropdown-item>
              <router-link to="/front/person">
                个人中心
              </router-link>
            </el-dropdown-item>
            <el-dropdown-item @click.native="logout">安全退出</el-dropdown-item>

          </el-dropdown-menu>
        </el-dropdown>
      </div>

      <div id="toggleBar" class="bar"><img
        src="http://ccnuer114282.gitee.io/education_platform/dist/static/img/nav.b716be73.png" alt="" class="nav-logo">
      </div>

    </div>
    <div class="containers">
      <img @click="dark" src="@/assets/png/moom.png" alt="" class="imgStyle"/>
      <router-view/>
    </div>

    <!--    滑到顶部-->
    <el-backtop target=".containers" :visibility-height="100" class="backTopStyle"  style="right:3px">
      <div
        style="{
            z-index:1;
            height: 50%;
            width: 50%;
            text-align: center;
            line-height: 40px;
            color: #1989fa; }">
        <!--        banner-->
        <img src="../../assets/png/up.png" class="backImgStyle"/>
      </div>
    </el-backtop>
  </div>
</template>
<script>
export default {
  data() {
    return {
      isActive: true,
      isError: false,
      xd_flag: false,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      pageWidth: 0,
      fullWidth: 0,
      isShow: false,
      resize: null,
    }
  },
  // watch: {
  //   fullWidth: {
  //     handler() {
  //       this.handleResize()
  //     },
  //     deep: true
  //   }
  // },
  computed: {

    index() {
      // vue-router 激活css高亮属性.nav-active
      //事先要根据自己的需求定义好路由
      let path = this.$route.name;
      switch (path) {
        case 'Home':
          return 1;
        case 'studentExam':
          return 2;
        case 'video':
          return 3;
        case 'SubPaper':
          return 3;
        case 'Journal':
          return 4;
        case 'Policy':
          return 5;
        case 'Patent':
          return 6;
        case 'Conference':
          return 7;
        case 'Recommendation':
          return 9;
        case 'home':
          return 10;
        case 'person':
          return 11;
      }
    }
  },
  created() {
    this.handleResize()
  },
  beforeDestroy: function () {

    window.removeEventListener('resize', this.handleResize)
  },
  mounted() {

    /* if (!this.resize) {
       this.resize = window.addEventListener('resize', this.handleResize)
     }*/


    // this.screenWidth = document.body.clientWidth
    /*window.onresize = () => {
      // console.log('高度');
      //屏幕尺寸变化
      return (() => {
        this.screenWidth = document.body.clientWidth
      })()
    }*/


    let that = this;
    let toggleBar = document.getElementById('toggleBar');
    let MenubarUl = document.querySelector('.menubar ul');
    let LeftSildeMenubar = document.querySelector('.menubar ul');

    //显示侧滑导航栏
    toggleBar.onclick = function () {
      LeftSildeMenubar.classList.toggle('active');
      document.body.style.overflow = 'hidden';
    }

    //隐藏策划导航栏
    MenubarUl.onclick = function () {
      LeftSildeMenubar.classList.remove('active');
      document.body.style.overflow = 'scroll';
    }

    //吸顶
    window.onscroll = function () {
      var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      if (scrollTop == 0) {
        that.xd_flag = false;
      } else {
        that.xd_flag = true;
      }
    }
  },
  methods: {
    dark() {
      //
      // this.$refs.frontDark.style='background-color: black;color: white;'
      // console.log(this.$refs)
      this.isActive = !this.isActive;
      this.isError = !this.isError;

    },
    handleResize(event) {
      this.fullWidth = document.documentElement.clientWidth;
      // 页面宽度小于1000px时
      if (this.fullWidth < 1000) {
        this.isShow = true;
      } else {
        this.isShow = false;
      }
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      localStorage.removeItem("user")
      localStorage.removeItem("menus")
      sessionStorage.removeItem("userid")
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      this.$message({
        type: 'success',
        message: '退出成功'
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.backTopStyle {
  //margin-right: 2px;
  width: 25px;
  height: 25px;;
  float: right;
  right:-40px;
  //position: relative;
  position:fixed;
  bottom: 60px;
  //left: 0
}



.error {
  background-color: black;
  color: white;
}

#continers {
  margin-top: 59px;
  .containers {

    height: 90vh;
    //overflow-y: scroll;
    overflow-x: hidden
  }



  .backImgStyle {
    position: absolute;
    width: 25px;
    height: 25px;
    top: -1px;
    right: -1px
  }

  .imgStyle {
    float: right;
    border-radius: 20px;
    width: 25px;
    position: fixed;
    right: 0px;
    bottom: 100px;
    padding-left: 2px;
    background-color: white
  }



  .nav-active {
    color: white !important;
    font-weight: bold !important;
    border-bottom: 2px solid white !important;
    box-sizing: border-box !important;
    height: 60px;
    transition: none !important;
  }

  .nav-logo {
    display: none;
    width: 30px;
    margin-top: 15px;
    vertical-align: middle;
    margin-left: 15px;
  }

  .xd-active {
    opacity: 1 !important;
    box-shadow: 0 0 6px #0d0d0dab;
  }

  .xys-info {
    display: none;
  }

  .content {
    width: 100%;
    height: 100%;
    text-align: center;
  }

  .menubar {
    background: #282c34;
    position: fixed;
    width: 100%;
    top: 0;
    height: 60px;
    display: flex;
    flex-direction: row;
    user-select: none;
    z-index: 100;
  }

  .nav-active[data-v-35a2e4f9] {
    color: white !important;
    font-weight: bold !important;
    border-bottom: 2px solid white !important;
    -webkit-box-sizing: border-box !important;
    box-sizing: border-box !important;
    height: 60px;
    -webkit-transition: none !important;
    transition: none !important;
  }

  .menubar-bg {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    z-index: -1;
    //background: linear-gradient(-150deg, #389c8d, #3c4f7e);
    opacity: 0;
    transition: opacity 0.6s ease;
  }

  .menubar .logo {
    min-width: 120px;
    width: 10%;
    text-align: center;
  }

  .menubar .logotitle {
    width: 100%;
    line-height: 60px;
    font-size: 24px;
    color: #fff;
  }

  .menubar .user {
    width: 20%;
    min-width: 80px;
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    margin-right: 30px;
    align-items: center;
    cursor: pointer;
  }

  .menubar .user .userimg {
    width: 60px;
    height: 60px;
  }

  .menubar .user .userimg img {
    width: 36px;
    height: 36px;
    margin-top: 12px;
    margin-right: 0px;
    border-radius: 50%;
  }

  .menubar .user .username {
    font-size: 14px;
    color: #eee;
    height: 60px;
    line-height: 60px;
  }

  .menubar ul {
    position: relative;
    width: 70%;
    height: 100%;
    display: flex;
    flex-direction: row;
    justify-content: left;
    text-align: center;
  }

  .menubar ul li {
    min-width: 50px;
    list-style: none;
    margin-left: 20px;
    transition: all 0.2s ease;
    cursor: pointer;
  }

  .menubar ul li a {
    position: relative;
    display: block;
    width: 100%;
    line-height: 60px;
    text-decoration: none;
  }

  .menubar ul li a:hover span {
    transition: all 0.3s ease;
    color: #eee;
  }

  .menubar ul li a .icon {
    display: none;
  }

  .menubar ul li a .title {
    position: relative;
    display: block;
    line-height: 60px;
    text-align: center;
    font-size: 16px;
    color: #bbb;
  }

  .list {
    margin-left: 5%;
    margin-right: 5%;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    text-align: center;
    justify-content: space-around;
    align-items: center;
  }

  .list .item {
    flex-basis: 23%;
    margin-top: 22px;
    height: 260px;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    text-align: center;
    justify-content: center;
    align-items: center;
    border-radius: 4px;
    transition: .314s;
    cursor: pointer;

  }

  .list .item:hover {
    transform: scale(1.02);
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12);
  }

  .list .item .desc {
    width: 90%;
    margin: 16px;
    flex-basis: 80%;
    background-color: #f5f5f5;
  }

  .list .item .title {
    width: 90%;
    margin: auto 16px;
    margin-bottom: 16px;
    flex-basis: 20%;
    background-color: #f5f5f5;
  }

  .footer {
    position: relative;
    bottom: 0;
    height: 172px;
    color: #a5a5a5;
    display: flex;
    text-align: center;
    align-items: center;
    justify-content: center;
    line-height: 36px;
  }

  /* 响应式 */
  @media (max-width: 1060px) {
    .nav-logo {
      display: block;
    }
    .nav-active {
      color: white !important;
      font-weight: bold !important;
      border-bottom: none !important;
      box-sizing: border-box !important;
      height: 60px;
      transition: all 0.1s ease-in;
    }

    .xys-info {
      display: block;
      margin-top: 20px;
      color: #aaa;

      img {
        width: 60px;
        margin-bottom: 10px;
      }

      hr {
        width: 80%;
        margin-left: 10%;
        margin-top: 20px;
        margin-bottom: 20px;
        border: none;
        height: 2px;
        border-radius: 10rem;
        background: #ccc;
      }

      div {
        cursor: pointer;
      }
    }
    .menubar {
      position: fixed;
      top: 0;
      display: block;
      height: 60px;
      transition: .321s;
    }

    .menubar .logo {
      position: absolute;
      width: 100%;
      top: 0;
      text-align: center;
    }

    .menubar .logotitle {
      width: 100%;
      line-height: 60px;
      font-size: 20px;
      color: #fff;
    }

    .menubar ul {
      width: 0px;
      position: fixed;
      height: 100vh;
      left: -60px;
      display: flex;
      flex-direction: column;
      background-color: #282c34;
      overflow: hidden;
      transition: .141s;
      z-index: 10;
      box-shadow: 6px 0px 10px 0px rgba(0, 0, 0, 0.5);
    }

    .menubar ul.active {

      overflow-y: scroll;
      //margin-bottom: -200px;
      left: 0;
      width: 260px;
      transition: .314s;
    }

    .menubar ul.active::after {
      content: '';
      position: fixed;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
      background-color: rgba(0, 0, 0, .3);
      z-index: -1;
    }

    .menubar ul li {
      width: 100%;
    }

    .menubar ul li a {
      display: flex;
      flex-direction: row;
    }

    .menubar ul li a .icon {
      position: relative;
      display: block;
      min-width: 60px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 24px;
      display: block;
    }

    .menubar ul li a .title {
      position: relative;
      display: block;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 16px;
    }

    .menubar .bar {
      position: absolute;
      top: 0;
      right: 0;
      width: 60px;
      height: 60px;
      line-height: 60px;
      cursor: pointer;
    }

    .menubar .user .username {
      display: none;
    }

    .list .item {
      flex-basis: 45%;
      margin-top: 12px;
      background-color: #fff;
      display: flex;
      flex-direction: column;
    }
  }

  @media (max-width: 500px) {
    #xxx {
      //margin-bottom: 2000px
    }
    .list .item {
      flex-basis: 100%;
      margin-top: 12px;
      background-color: #fff;
      display: flex;
      flex-direction: column;
    }
  }
}
</style>
